package CoteServeur;

import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Vector;

public class Emission implements Runnable {

	private PrintWriter out;
	private String message = null;
	private Scanner sc = null;
	private String typeEmis;
	private ControleurServeur controleur;

	public Emission(PrintWriter out, String mess, String typeEmis,
			ControleurServeur cont) {
		this.out = out;
		message = mess;
		this.typeEmis = typeEmis;
		controleur = cont;
	}

	public void run() {

		if (typeEmis == "mess") {
			out.println(message);
			out.flush();

		} else if (typeEmis.equals("ValEnvoiMess")) {
			out.println(typeEmis);
			out.flush();
			out.println(message);
			out.flush();
			out.println("fin");
			out.flush();
		} else if (typeEmis.equals("EnvoiMess")) {
			out.println(typeEmis);
			out.flush();
			out.println(message);
			out.flush();
			out.println("fin");
			out.flush();
		} else if (typeEmis.equals("dial")) {
			out.println(typeEmis);
			out.flush();
			out.println(message);
			out.flush();
			out.println("fin");
			out.flush();
		} else if (typeEmis.equals("statutChg")) {
			out.println(typeEmis);
			out.flush();
			out.println(message);
			out.flush();
			out.println("fin");
			out.flush();
		} else if (typeEmis.equals("valConec")) {
			out.println("dial");
			out.flush();
			out.println("validation Connection");
			out.flush();
			// out.println(message);
			// out.flush();
			String[] ch = controleur.decoupe(message);
			
			out.println("type Compte : " + "&°&" + ch[1]);
			out.flush();
			System.out.println(message);
			
			out.println("statut Compte : " + "&°&" + ch[2]);
			out.flush();
			System.out.println(ch[2]);

			Vector<String> vec = controleur.getComptes().get(ch[0])
					.getVecContact();
			if (vec.size() > 0) {
				Vector<String> vectrie = new Vector<String>();
				for (int i = 0; i < vec.size(); i++) {
					if (controleur.getComptes().get(vec.get(i)).getStatut() == 0) {
						vectrie.addElement("+ " + vec.get(i));
					}
				}
				for (int i = 0; i < vec.size(); i++) {
					if (controleur.getComptes().get(vec.get(i)).getStatut() == 1) {
						vectrie.addElement("# " + vec.get(i));
					}
				}
				for (int i = 0; i < vec.size(); i++) {
					if (controleur.getComptes().get(vec.get(i)).getStatut() == 2) {
						vectrie.addElement("- " + vec.get(i));
					}
				}

				for (int i = 0; i < vec.size(); i++) {
					out.println(vectrie.get(i));
					out.flush();
				}
			}
			out.println("fin");
			out.flush();
		} else if (typeEmis.equals("deco")) {
			out.println(typeEmis);
			out.flush();
			out.println(message);
			out.flush();
			out.println("fin");
			out.flush();
		} else if (typeEmis.equals("addCont")) {
			out.println(typeEmis);
			out.flush();
			out.println(message);
			out.flush();

			Vector<String> vec = controleur.getComptes().get(message).getVecContact();
			if (vec.size() > 0) {
				Vector<String> vectrie = new Vector<String>();
				for (int i = 0; i < vec.size(); i++) {
					if (controleur.getComptes().get(vec.get(i)).getStatut() == 0) {
						vectrie.addElement("+ " + vec.get(i));
					}
				}
				for (int i = 0; i < vec.size(); i++) {
					if (controleur.getComptes().get(vec.get(i)).getStatut() == 1) {
						vectrie.addElement("# " + vec.get(i));
					}
				}
				for (int i = 0; i < vec.size(); i++) {
					if (controleur.getComptes().get(vec.get(i)).getStatut() == 2) {
						vectrie.addElement("- " + vec.get(i));
					}
				}

				for (int i = 0; i < vec.size(); i++) {
					out.println(vectrie.get(i));
					out.flush();
				}
			}
			out.println("fin");
			out.flush();
		} else if (typeEmis.equals("infCont")) {
			out.println(typeEmis);
			out.flush();
			out.println(message);
			out.flush();
			out.println("fin");
			out.flush();
		} else if (typeEmis.equals("modifMdp")) {
			out.println(typeEmis);
			out.flush();
			out.println(message);
			out.flush();
			out.println("fin");
			out.flush();
		} else if (typeEmis.equals("actuCont")) {
			out.println(typeEmis);
			out.flush();
			System.out.println("dans emission actu cont");
			Vector<String> vec = controleur.getComptes().get(message).getVecContact();
			if (vec.size() > 0) {
				Vector<String> vectrie = new Vector<String>();
				for (int i = 0; i < vec.size(); i++) {
					if (controleur.getComptes().get(vec.get(i)).getStatut() == 0) {
						vectrie.addElement("+ " + vec.get(i));
					}
				}
				for (int i = 0; i < vec.size(); i++) {
					if (controleur.getComptes().get(vec.get(i)).getStatut() == 1) {
						vectrie.addElement("# " + vec.get(i));
					}
				}
				for (int i = 0; i < vec.size(); i++) {
					if (controleur.getComptes().get(vec.get(i)).getStatut() == 2) {
						vectrie.addElement("- " + vec.get(i));
					}
				}

				for (int i = 0; i < vec.size(); i++) {
					out.println(vectrie.get(i));
					out.flush();
				}
			}
			out.println("fin");
			out.flush();
		} else {
			System.out.println("Erreur message non envoyé");
		}

	}
}